package com.amin.app.web.controller;

import com.amin.app.web.entity.Page;
import com.amin.app.web.entity.SDict;
import com.amin.app.web.entity.SDictList;
import com.amin.app.web.service.DictListService;
import com.amin.app.web.service.DictService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSessionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.jws.WebParam;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 字典控制器
 * 20210717
 */
@Controller
@RequestMapping(value = "/dict")
public class DictController {
    final
    DictService dictService;
    final
    DictListService dictListService;
    public DictController(DictService dictService, DictListService dictListService) {
        this.dictService = dictService;
        this.dictListService = dictListService;
    }

    /**
     * 获取字典列表
     * @param model
     * @param page 分页工具类
     * @return
     */
    @RequestMapping(value="/getDictList")
    public String getDictList(Model model, Page page,SDict sDict){
        List<SDictList> sys_switch_type = dictListService.getDictListByDictType("sys_switch_type");
        model.addAttribute("systemSwitchType",sys_switch_type);
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<SDict> dictList = dictService.getDictList(sDict);
        PageInfo<SDict> dictListPage = new PageInfo<>(dictList);
        model.addAttribute("dictList",dictListPage);
        model.addAttribute("sDict",sDict);
        return "/amin/dict/dictList";
    }

    /**
     * 添加字典/编辑字典
     * @param model
     * @param sDict
     * @return
     */
    @RequestMapping(value = "/createDict")
    public String createDict(Model model,SDict sDict){
        List<SDictList> sys_switch_type = dictListService.getDictListByDictType("sys_switch_type");
        model.addAttribute("systemSwitchType", sys_switch_type);
        sDict.setDictState("0");
        if (null != sDict.getId() && 0 != sDict.getId()){
            sDict = dictService.getDictById(sDict.getId());
        }
        model.addAttribute("dict", sDict);
        return "/amin/dict/createDict";
    }

    /**
     * 数据提交
     * @param sDict
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/dataSubmit")
    public Map<Object,Object> dataSubmit(SDict sDict){
        Map<Object,Object> answer = new HashMap<>();
        try {
            if (null == sDict.getId() || 0 ==  sDict.getId()){
                sDict.setCreatedTime(new Date());
                dictService.save(sDict);
            }else {
                dictService.update(sDict);
            }
            answer.put("code",0);
            answer.put("msg","数据保存成功");
        }catch (SqlSessionException e){
            answer.put("code",1);
            answer.put("msg",e.getMessage());
        }
        return answer;
    }


    /**
     * 删除字典
     * @param model
     * @param sDict
     * @return
     */
    @RequestMapping(value = "/delById")
    public String delById(Model model,SDict sDict){
        dictService.delById(sDict.getId());
        return "redirect:/dict/getDictList";
    }
}
